package com.zzptc.mzb.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.util.Log;

import com.zzptc.mzb.myapplication.adapter.MyRecyclerviewAadpter;
import com.zzptc.mzb.myapplication.databinding.ActivityMainBinding;
import com.zzptc.mzb.myapplication.entity.RecipeModel;
import com.zzptc.mzb.myapplication.entity.ResultBean;
import com.zzptc.mzb.myapplication.service.MyAppService;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    List<RecipeModel>recipeModels=new ArrayList<>();
    ActivityMainBinding binding;

    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("https://liyuankun.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    MyAppService myAppService = retrofit.create(MyAppService.class);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
        binding.rvRecipe.setLayoutManager(linearLayoutManager);


        getAllRecipte();

    }

    private void getAllRecipte() {
        Call<RecipeModel> recipe=myAppService.getRecipe(100);
        recipe.enqueue(new Callback<RecipeModel>() {

            @Override
            public void onResponse(Call<RecipeModel> call, Response<RecipeModel> response) {
                List<ResultBean> list;
                RecipeModel re=response.body();
                list=re.getResults();

                MyRecyclerviewAadpter myRecyclerviewAadpter = new MyRecyclerviewAadpter(list, MainActivity.this);
                binding.rvRecipe.setAdapter(myRecyclerviewAadpter);
            }

            @Override
            public void onFailure(Call<RecipeModel> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());

            }
        });
    }
}
